package com.mycom;

import java.sql.*;

//jdbc:java database connection
public class Main2 {
    public static void main(String[] args) {

        try {
            //1 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2 获得连接   Connection是接口 实际获得的就是针对MYSQL进行连接的
            Connection cn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/newdb","root","root");

            System.out.println(cn);

            //查询
            String query = " select * from student where stusex=? and stuage < ?";
            PreparedStatement ps  = cn.prepareStatement(query);
            ps.setString(1,"male");
            ps.setInt(2,25);
            //查询结果为 结果集
            ResultSet rs = ps.executeQuery();
            while(rs.next()){//遍历结果集的每一行
                //取出每行中的每个列
                String result = rs.getInt("sid")+" "+
                                rs.getString("stuno")+" "+
                                rs.getString("stuname")+" "+
                                rs.getInt("stuage")+" "+
                                rs.getString("stubirth");
                System.out.println(result);
            }
            cn.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}